<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Posts_model extends CI_Model {

    private $tbl_posts = 'posts';
    private $_table = 'posts';

    public function __construct() {
        parent::__construct();
    }
    
     public function get_count_all() {
        
        $this->db->select('COUNT(*) AS count');
        $this->db->where("post_category > ", "0");
       $result =  $this->db->get($this->_table)->row();
        //echo $result;exit();
        return $result->count;
    }

    /**
     * 获取文章列表
     * @param   int     $num 单面显示文章数量
     * @param   int     $offset 结果集偏移量
     * @return  array   
     */
    
    
    
    public function get_posts($num, $offset, $where = null) {
        $this->db->order_by("post_date", "desc");
        $this->db->where("post_category > ", "0");
        if($where) {
            $query = $this->db->get_where($this->_table, $where, $num, $offset);
        } else {
            $query = $this->db->get($this->_table, $num, $offset);
        }
         
        return $query->result();
    }

    /**
     * 获取单个
     * @param   int $ID 文章ID
     * @return  array
     */
    public function get_post_by_id($ID) {
        $query = array();
        if (intval($ID) > 0)
            $query = $this->db->get_where($this->tbl_posts, array('ID' => intval($ID)))->row();
        return $query;
    }

    /**
     * 获取上一篇文章 
     * @param int $ID 文章ID
     * @return array 
     */
    public function get_pre_post($ID) {
        $query = array();
        if (intval($ID) > 0) {
            $this->db->select('ID,post_title');
            $this->db->limit(1);
            $this->db->order_by('ID asc');
            $this->db->where("post_category > ", "0");
            $query = $this->db->get_where($this->tbl_posts, array('ID > ' => intval($ID)))->row();
        }
        return $query;
    }

    /**
     * 获取下一篇文章 
     * @param int $ID 文章ID
     * @return array 
     */
    public function get_next_post($ID) {
        $query = array();
        if (intval($ID) > 0) {
            $this->db->select('ID,post_title');
            $this->db->limit(1);
            $this->db->where("post_category > ", "0");
            $this->db->order_by('ID desc');
            $query = $this->db->get_where($this->tbl_posts, array('ID < ' => intval($ID)))->row();
        }
        return $query;
    }

    /**
     * 获取文章描述
     * @param string $content 文章内容
     * @return string 处理后的文章内容
     */
    function get_post_description($content) {
        return mb_substr(str_replace("\n", "", strip_tags($content)), 0, 400, 'utf-8');
    }

    /*
     * 更新文章
     * @param int $ID
     * @param array $post
     * @return 
     * */

    function update($ID) {
        $this->load->library('BBcode');

        $post = array(
            'post_title' => $this->input->post('post_title'),
            'post_content' => $this->input->post('post_content'),
            'post_content_html' => $this->bbcode->BBCode2Html($this->input->post('post_content')),
             'post_modified' => date('Y-m-d H:i:s')
            
        );
        //$this->db->set('post_modified', 'NOW()', FALSE);
        $this->db->where('ID', $ID);
        $this->db->update($this->tbl_posts, $post);
    }

    function create() {
        $this->load->library('BBcode');
        $post = array(
            'post_title' => $this->input->post('post_title'),
            'post_content' => $this->input->post('post_content'),
            'post_content_html' => $this->bbcode->BBCode2Html($this->input->post('post_content')),
            'post_date' => date('Y-m-d H:i:s')
        );
        //$this->db->set('post_date', 'NOW()', FALSE);
        $this->db->insert($this->tbl_posts, $post);
        return $this->db->insert_id();
    }

}
